package com.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dao.CourseDao;
import com.opensymphony.xwork2.ActionContext;
import com.po.Class_coursePO;
import com.po.CoursePO;
import com.po.Course_skillPO;
import com.po.SkillPO;
@Controller
public class LevelAction {
	@Autowired
	private CourseDao cd;
	private List<SkillPO> listSkill;
	
	/**
	 * 展示等级中的详细的技能信息
	 * @return
	 */
	public String show(){
		//拿到班级内详细级别分类的ID 
		HttpServletRequest request = ServletActionContext.getRequest();
		int id;
		if (request.getParameter("cid") != null) {
			 id = Integer.parseInt(request.getParameter("cid"));
		}else{
		     id = (int) request.getAttribute("vid");	
		}
		//给request域中保存课程的id
		request.setAttribute("id", id);
		
		listSkill = cd.findSkillPO(id);
		ActionContext.getContext().put("listSkill", listSkill);
		
		return "course_detail";
	}
	
	/**
	 * 删除课程指定的等级
	 */
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("id"));
		int pid = Integer.parseInt(request.getParameter("pid"));
		//删除课程等级
		cd.deleteLevelInfo(id);
		//将课程的id放进去
		request.setAttribute("pid", pid);
		return "after_delete";
	}
	/**
	 * 给课程等级信息中保存相应的技能
	 * @return
	 */
	public String saveInfo() {
		// 保存技能在数据库中的技能表中，并拿到插入技能的ID值
		HttpServletRequest request = ServletActionContext.getRequest();
		SkillPO skillPO = new SkillPO();
		skillPO.setSkill_name(request.getParameter("name"));
		skillPO.setSkill_content(request.getParameter("intro"));
		cd.InsertSkillInfo(skillPO);

		int skillId = skillPO.getSkill_id();
		// 拿到的课程的ID值
		int courseId = Integer.parseInt(request.getParameter("id"));

		// 将课程ID和技能ID关联起来保存在第三张关联表中
		Course_skillPO conn = new Course_skillPO();
		conn.setCourse_id(courseId);
		conn.setSkill_id(skillId);

		cd.insertConnection(conn);

		return null;
	}
	
	/**
	 * 修改等级信息时回显数据
	 */
	public String updateLevelEcho(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("id"));
		int pid = Integer.parseInt(request.getParameter("pid"));
		CoursePO coursePO = new CoursePO();
		coursePO = cd.findLevelEchoById(id);
		request.setAttribute("id", id);
		request.setAttribute("pid", pid);
		ActionContext.getContext().put("coursePO", coursePO);
		return "updateLevelEcho";
	}
	
	
	
	
	/**
	 * 修改课程中的等级信息
	 * @return
	 */
	public String updateLevel() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", request.getParameter("id"));
		map.put("name", request.getParameter("name"));
		map.put("intro", request.getParameter("intro"));
		
		cd.updateLevel(map);
		
		return null;
	}
	
	/**
	 * 只要是空跳界面，添加新的技能
	 * @return
	 */
	public String preAddLevel(){
		HttpServletRequest request = ServletActionContext.getRequest();
		ActionContext.getContext().put("pid", request.getParameter("pid"));
		return "add_level";
	}
	
	/**
	 * 添加新的课程等级信息
	 * @return
	 */
	public String addLevel(){
		  
		HttpServletRequest request = ServletActionContext.getRequest();
		CoursePO coursePO = new CoursePO();
		coursePO.setCourse_name(request.getParameter("name"));
		coursePO.setCourse_content(request.getParameter("intro"));
		cd.saveLevelInfo(coursePO);
		//将课程等级ID和课程的ID关联起来，保存在第三那张表中
		int class_id = Integer.parseInt(request.getParameter("pid"));
		int course_id = coursePO.getId();
		Class_coursePO conn = new Class_coursePO();
		conn.setClass_id(class_id);
		conn.setCourse_id(course_id);
		cd.addClassCourse_connection(conn);
		
		return null;
	}
	
	
	
	
}
